package hu.blok1.veilingsite.server;

import hu.blok1.veilingsite.client.data.HomeService;
import hu.blok1.veilingsite.shared.Product;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class HomeServiceImpl extends RemoteServiceServlet implements
		HomeService {

	@Override
	public List<Product> GetProducts(String column, String value) {
		List<Product> list = null;
		if (column.equals("SEARCH")) {
			list = GetProductObject.GetMultiProduct(DBConnector
					.GetResultSet("SELECT * FROM product WHERE name like '%"
							+ value + "%' OR description LIKE '%" + value
							+ "%' AND status = 'Active'"));
		} else {
			list = GetProductObject.GetMultiProduct(DBConnector
					.GetResultSet("SELECT * FROM product WHERE " + column
							+ " = '" + value + "' AND status = 'Active'"));
		}
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				try {
					String queryGetMaxBid = "SELECT MAX(bid) AS \"highestBid\" FROM bids WHERE fk_product_id = "
							+ list.get(i).getProductID();
					ResultSet rsGetMaxBid = DBConnector
							.GetResultSet(queryGetMaxBid);
					while (rsGetMaxBid.next()) {
						if (rsGetMaxBid.getInt("highestBid") > 0) {
							list.get(i).setHighestBid(
									rsGetMaxBid.getInt("highestBid"));
						} else {
							list.get(i).setHighestBid(
									list.get(i).getMinimumPrize());
						}
					}
					rsGetMaxBid.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		DBConnector.CloseAll();
		return list;
	}

	public List<Product> Get10LatestProducts() {
		List<Product> list = null;
		list = GetProductObject
				.GetMultiProduct(DBConnector
						.GetResultSet("SELECT * FROM product WHERE status = 'Active' ORDER BY STARTDATE DESC"));
		for (int i = 0; i < list.size(); i++) {
			try {
				String queryGetMaxBid = "SELECT MAX(bid) AS \"highestBid\" FROM bids WHERE fk_product_id = "
						+ list.get(i).getProductID();
				ResultSet rsGetMaxBid = DBConnector
						.GetResultSet(queryGetMaxBid);
				while (rsGetMaxBid.next()) {
					if (rsGetMaxBid.getInt("highestBid") > 0) {
						list.get(i).setHighestBid(
								rsGetMaxBid.getInt("highestBid"));
					} else {
						list.get(i)
								.setHighestBid(list.get(i).getMinimumPrize());
					}
				}
				rsGetMaxBid.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		DBConnector.CloseAll();
		return list;
	}

	public List<Product> Get10ClosestToEnding() {
		List<Product> list = null;
		list = GetProductObject
				.GetMultiProduct(DBConnector
						.GetResultSet("SELECT * FROM product WHERE status = 'Active' ORDER BY ENDDATE ASC"));
		for (int i = 0; i < list.size(); i++) {
			try {
				String queryGetMaxBid = "SELECT MAX(bid) AS \"highestBid\" FROM bids WHERE fk_product_id = "
						+ list.get(i).getProductID();
				ResultSet rsGetMaxBid = DBConnector
						.GetResultSet(queryGetMaxBid);
				while (rsGetMaxBid.next()) {
					if (rsGetMaxBid.getInt("highestBid") > 0) {
						list.get(i).setHighestBid(
								rsGetMaxBid.getInt("highestBid"));
					} else {
						list.get(i)
								.setHighestBid(list.get(i).getMinimumPrize());
					}
				}
				rsGetMaxBid.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		DBConnector.CloseAll();
		return list;
	}
}